import java.util.ArrayList;
import java.util.List;

public class 全排列 {
    public static List<List<Integer>> ans;
    public static List<Integer> path;
    public static int n;
    public static boolean[] vis;

    public List<List<Integer>> permute(int[] nums) {
        n = nums.length;
        ans = new ArrayList<>();
        path = new ArrayList<>();
        vis = new boolean[n];
        // 交给 dfs
        dfs(nums,0);
        return ans;
    }

    public void dfs(int[] nums,int pos) {
        if (pos == n) {
            ans.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0;i < n;i++) {
            if(vis[i]) {
                continue;
            }
            path.add(nums[i]);
            vis[i] = true;
            dfs(nums,pos + 1);
            path.remove(path.size() - 1);
            vis[i] = false;
        }
    }
}
